#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<set>
#include<vector>
using namespace std;

class Solution
{
public:
    int thirdMax(vector<int>& nums)
    {
        set<int> s;
        for (int i = 0; i < nums.size(); i++)
        {
            s.insert(nums[i]);
            if (s.size() > 3) s.erase(s.begin());
        }
        return s.size() == 3 ? *s.begin() : *--s.end();
    }
};

class Solution
{
public:
    int longestOnes(vector<int>& nums, int k)
    {
        int ret = 0, zero = 0;
        for (int left = 0, right = 0; right < nums.size(); right++)
        {
            if (nums[right] == 0) zero++;
            while (zero > k)
            {
                if (nums[left] == 0) zero--;
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};